//index.js
//获取应用实例
const app = getApp()


Page({
  data: {
    isJoin:false,

    joiner:0,

    data:null,
    banner:'',
    jpName:'',
    jpRequire:'',
    condition:'',
    
    lid:0,
    uid:0,
    owner:0,

  },
  //事件处理函数
  // bindViewTap: function() {
  //   wx.navigateTo({
  //     url: '../logs/logs'
  //   })
  // },
  onLoad: function (options) {
    var that = this
    var id = options.id;
    that.setData({
      lid:options.id
    })

    var timer = setInterval( ()=>{
      if(app.globalData.acc){
        that.setData({
          uid:app.globalData.acc.id,
          owner:app.globalData.acc.owner
        })
        that.getLotteryById(id , app.globalData.acc.id)
        clearInterval(timer)
      }
    } , 100)
    
  },
  onGotUserInfo: function(e) {
    var that = this
    // console.log(e.detail.errMsg)
    if(e.detail.errMsg == 'getUserInfo:ok'){
      console.log(app.globalData.acc)
      // userinfo存入全局 
      app.globalData.userInfo = e.detail.userInfo
      // 保存数据库
      wx.request({
        url: app.globalData.url + 'lotteryaccs/updateAccInfo',
        data: {
          openid:app.globalData.acc.openid,
          avatar_url:e.detail.userInfo.avatarUrl,
          nick_name:e.detail.userInfo.nickName
        },
        method: 'POST',
        success: (res)=>{
          console.log("updateAccInfo success")
          // console.log(res);
          
          // if(res.data.msg == 1){
            that.setData({
              isJoin:true
            })
            wx.request({
              url: app.globalData.url + 'lotterys/join_lottery',
              data: {
                lid:that.data.lid,
                uid:app.globalData.acc.id
              },
              method: 'POST',
              success: (res)=>{
                console.log("join_lottery success");
                console.log(res);
                that.getLotteryById(that.data.lid , app.globalData.acc.id)
              },
              fail: ()=>{
                console.log("join_lottery fail");
                
              }
            });
          // }
        },
        fail: ()=>{
          // wx.showToast({
          //   title: '参与失败，请重试',
          //   icon: 'none',
          //   duration: 1500,
          // });
        },
      });
    }
    if(e.detail.errMsg == 'getUserInfo:fail auth deny'){
      // console.log('fail')
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    that.getLotteryById(that.data.lid , app.globalData.acc.id)
    wx.stopPullDownRefresh()
  },
  // 根据id获取对应抽奖信息
  getLotteryById:function(lid , uid){
    var that = this
    wx.request({
      url: app.globalData.url + 'lotterys/get_lottery',
      data: {
        lid:lid,
        uid:uid
      },
      method: 'POST',
      success: (res)=>{
        // console.log(res)
        that.setData({
          data:res.data.data,
          owner:res.data.data.owner
        })
        if(res.data.data.type == 0){
          // 开奖条件为时间
          that.setData({
            condition: "到 " + this.toDate(res.data.data.condition) + " 时自动开奖"
          })
        }
        if(res.data.data.type == 1){
          // 开间条件为人数
          that.setData({
            condition:"人数满 " + res.data.data.condition + " 人时自动开奖"
          })
        }
        if(res.data.msg){
          this.setData({
            isJoin:true
          })
        }
        that.setData({
          banner:res.data.data.cover,
          jpName:res.data.data.title,
          jpRequire:res.data.data.detail,
          joiner:res.data.data.user_count,
          lucker:res.data.data.lucker
        })
      },
      fail: ()=>{
        // that.getLotteryById(id)
      }
    });
  },

  // 时间戳转时间
  toDate:function(number){
      var n=number * 1000;
      var date = new Date(n);
      var Y = date.getFullYear() + '-';
      var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
      var D = date.getDate()<10 ? '0'+date.getDate() : date.getDate();
      
      var h = date.getHours() + '-';
      var m = date.getMinutes();

      return (Y + M + D + ' ' + h + m)
  },

  //
  settimeout:function(){
    var timer = setInterval( ()=> {
      // console.log("----")
      if(app.globalData.acc){
        // console.log(app.globalData.acc);
        clearInterval(timer)
      }
    } , 100)
  },

  onShareAppMessage:function(){
    return {
      title:this.data.data.title,
      path:'/pages/detail/detail?id='+this.data.data.id,
      imageUrl: this.data.data.cover
    }
  }
  
})
